package com.asdc.demo.durable;

import com.asdc.demo.utils.RabbitMqUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

/**
 * @ClassName ConsumerAck01
 * @Author bsy
 * @Description MQ内部消息可靠性 - 持久化
 *  模拟场景：持久化
 *
 * @Date 2022/3/12 15:55
 * @Version 1.0
 */
public class ConsumerDura01 {

    private static final String QUEUE_NAME = "QUEUE_DUR_VVYX";

    public static void main(String[] args) throws Exception {
        Channel channel = RabbitMqUtils.getChannel();
        DeliverCallback deliverCallback = (consumerTag, delivery) -> {
            String receivedMessage = new String(delivery.getBody());
            System.out.println("ConsumerDura01 接收到消息:" + receivedMessage);
            /**
             * 1.消息标记 tag
             * 2.是否批量应答未应答消息
             */
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);
        };
        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println(consumerTag + "ConsumerDura01 消费者取消消费接口回调逻辑");
        };
        System.out.println("ConsumerDura01 消费者启动等待消费.................. ");
        //false代表手动应答
        channel.basicConsume(QUEUE_NAME,false,deliverCallback,cancelCallback);
    }
}
